/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver;

import java.lang.Math;
/**
 *
 * @author Michael Ferry
 */
public class SensorComputationsServer {
    //private static int pHORP_serial=174418;
    //private static int HeaterSensor_serial=106721;
    
    public static double ComputeSensorValue(int SerialNumber,int SensorNumber, int SensorRawValue){
        //System.out.println(SerialNumber);
        if(SerialNumber == WWControlServer.ifk_pHORP_serial){
            //sensors attached to this device (positions 0-7)
            //Sensor Input 1: Phidgets 1130 pH/ORP adapter - set to pH
            //Sensor Input 2: Phidgets 1130 ORP adapter - set to ORP
            //Sensor Input 3: Phidgets 1124 Precision temperature sensor
            if(SensorNumber == 1){
                return ComputePHmV(SensorRawValue);
            }else if(SensorNumber == 2){
                return ComputeORPmV(SensorRawValue);
            }else if(SensorNumber == 3){
                return Compute1124Temp(SensorRawValue);
            }    
        }else if(SerialNumber == WWControlServer.ifk_HeaterSensor_serial){
            if(SensorNumber == 0 || SensorNumber == 1 || SensorNumber == 2 || SensorNumber == 3){
                return ComputeTemperature2252ohms(SensorRawValue);
            }
                
        }
        return SensorRawValue;
    }
          
    public static double ComputePHmV(int SensorRawValue){
            //sensor ranges from -400mV to 400mV
            return 0.8008*(SensorRawValue/4.095) - 400.8;
            //return 0.0178*(SensorRawValue/4.095) - 1.889;
    }
    public static double ComputeORPmV(int SensorRawValue){
            //sensor ranges from -2000mV to 2000mV
            return  1000*(2.5 - (SensorRawValue/4.095)/200)/1.037;
    }
    public static double Compute1124Temp(int SensorRawValue){
        return (SensorRawValue/4.095)*0.222 - 61.111;
    }
            
    public static double ComputeTemperature2252ohms(int SensorRawValue) {
        //double PhidgetsRawSensorResolution=4096;
        double Temperature;
        double PhidgetsVoltDividerResistance = 2610;
        int SensorResistanceAt25 = 2252;
        
        double SensorResistance =  PhidgetsVoltDividerResistance*((WWControlServer.PhidgetsRawSensorDynRange/SensorRawValue)-1);
        
        Temperature = 25 - Math.log(SensorResistance/SensorResistanceAt25)/Math.log(1.04);
         
         //System.out.println("Raw: " + SensorRawValue + "SR: " + SensorResistance+ "Temp: "+ Temperature);
        return Temperature;
    }
    
}
